package org.liang.factory.factory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;

import org.liang.factory.bean.BeanInfo;
import org.liang.factory.code.CreateExampleJavaCode;
import org.liang.factory.util.TemplateUtil;

import freemarker.template.Template;

public abstract class BeanFactory {

	public static void generateBean(BeanInfo beanInfo) {
		if(beanInfo==null) {
			return;
		}
		try {
			String outPath = beanInfo.getClassPath()+"\\"+beanInfo.getClassPackage().replace(".", "\\")+"\\"+beanInfo.getClassName()+".java";
			File file = new File(outPath);
			
			checkParentDir(file.getParent());//创建父目录
			
			//如果文件存在，并且不覆盖，不操作
			if(file.exists()) {
				if(beanInfo.cover) {
					FileOutputStream f = new FileOutputStream(outPath);
					Writer out = new OutputStreamWriter(f);
					Template template = TemplateUtil.getTemplate("javaBean.ftl");
					template.process(beanInfo, out);
					System.err.println("文件已存在，覆盖操作：" + outPath);
				}else {
					System.err.println("文件已存在，不生成：" + outPath);
				}
			}else {
				FileOutputStream f = new FileOutputStream(outPath);
				Writer out = new OutputStreamWriter(f);
				Template template = TemplateUtil.getTemplate("javaBean.ftl");
				template.process(beanInfo, out);
				System.out.println("文件新建路径：" + file.getAbsolutePath());
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*
	 * 校验路径是否存在，不存在创建
	 */
	private static void checkParentDir(String path) {
		File file = new File(path);
		if(!file.exists()) {
			file.mkdirs();
		}
	}
	public static void generateBeans(List<BeanInfo> beanInfo) {
		if(beanInfo==null) {
			return;
		}
		try {
			for (BeanInfo beanInfo2 : beanInfo) {
				generateBean(beanInfo2);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void printBean(BeanInfo beanInfo) {
		if(beanInfo==null) {
			return;
		}
		try {
			Writer out = new OutputStreamWriter(System.out);
			Template template = TemplateUtil.getTemplate("javaBean.ftl");
			template.process(beanInfo, out);
			out.flush();
			out.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void printBeans(List<BeanInfo> beanInfo) {
		if(beanInfo==null) {
			return;
		}
		try {
			for (BeanInfo beanInfo2 : beanInfo) {
				printBean(beanInfo2);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}	
	public static void main(String[] args) {
//		BeanInfo beanInfo = new BeanInfo();
//		beanInfo.setClassPath("D:\\workspace\\jixiang2\\factory-bean\\src\\main\\java");
//		beanInfo.setClassPackage("org.liang.factory.auto");
//		beanInfo.setClassName("Person");
//		
//		List<BeanAttrsInfo> attrList = new ArrayList<BeanAttrsInfo>();
//		attrList.add(new BeanAttrsInfo("name","String","注释"));
//		attrList.add(new BeanAttrsInfo("age","int","年龄"));
//		attrList.add(new BeanAttrsInfo("phone","String","手机"));
//		beanInfo.setAttrList(attrList);
//		generateBean(beanInfo);
		
		List<BeanInfo> list = CreateExampleJavaCode.createList();
		printBeans(list);
		
	}
}
